package com.dbs.interfaces.group.dao;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.dbs.interfaces.group.entity.User;

@CacheConfig(cacheNames = "users")
public interface TestDao extends JpaRepository<User, Integer> {
	/*
	 * 示例中缓存是注释在service层 实际dao或者service应该都可以。 缓存一个个标注太累 适合用aop
	 * 1.@CachePut表示缓存新添加的数据或者更新的数据到缓存中，两个参数value表示缓存的名称为people，
	 * key表示缓存的key为person的id 例如：@CachePut(value = "people", key = "#person.id")
	 * 2.@CacheEvict表示从缓存people中删除key为id的数据 @CacheEvict(value = "people")
	 * 3.@Cacheable表示添加数据到缓存中，缓存名称为people，缓存key为person的id属性。@Cacheable(value =
	 * "people", key = "#person.id")
	 */
	@Cacheable(key = "#p0", condition = "#p0.length() < 10")
	User findByName(String name);

	@CachePut(key = "#p0.name")
	User save(User user);

	@Query("from User u where u.name=:name")
	User findUser(@Param("name") String name);
}
